﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using SimpleCut.Data;
using SimpleCut.Config;
using SimpleCut.Control;
using Util;
using YLServerInterface.UserControls;
namespace SimpleCut
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            init();
        }
        public MainWindow(string workingDir)
            : this()
        {
            WorkingDir = workingDir;
        }
        public string WorkingDir;
        SizeItems sizeItems;
        private void init()
        {
            ConfigHelper ch = new ConfigHelper();
            sizeItems = ch.ReadConfig();
            ReloadSizeItemsListBox(sizeItems);
        }
        /// <summary>
        /// 重新加载配置列表
        /// </summary>
        /// <param name="si"></param>
        private void ReloadSizeItemsListBox(SizeItems si)
        {
            ConfigHelper ch = new ConfigHelper();
            ch.SaveConfig(si);
            lstSizeItems.Items.Clear();
            var controls = si.Items
                .OrderBy(config => config.Rank)
                .Select(config =>
                    {
                        var ctrol = new SizeItemCtrol(config);
                        ctrol.RankChanged += new Action<SizeItemConfig>(c => ReloadSizeItemsListBox(si));
                        ctrol.SizeItemDeleted += new Action<SizeItemConfig>(c =>
                        {
                            si.Items.Remove(c);
                            ReloadSizeItemsListBox(si);
                        });
                        return ctrol;
                    }
                 );
            foreach (var ctrol in controls)
            {
                lstSizeItems.Items.Add(ctrol);
            }
        }

        /// <summary>
        /// 添加新配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSizeItemConfigAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SizeItemConfig config = new SizeItemConfig()
                {
                    Name = txtSizeItemConfigName.Text,
                    Width = Convert.ToSingle(txtSizeItemConfigWidth.Text),
                    Height = Convert.ToSingle(txtSizeItemConfigHeight.Text),
                    Rank = Convert.ToInt32(txtSizeItemConfigRank.Text)
                };
                sizeItems.Items.Add(config);
                ReloadSizeItemsListBox(sizeItems);
            }
            catch { }
        }

        IEnumerable<string> dropedPaths;
        private void wrapPanelChangePosition1_Drop(object sender, DragEventArgs e)
        {
           // System.Diagnostics.Debugger.Launch();
            dropedPaths = getImgPaths(((string[])e.Data.GetData("FileDrop")), "*.*");
            loadimgs(dropedPaths);
        }
        public IEnumerable<string> getImgPaths(IEnumerable<string> paths, string searchPtn)
        {
            return paths
                   .SelectMany(path =>
                   Common.isDir(path)
                   ? Common.getDirFilesExt(path, searchPtn, System.IO.SearchOption.AllDirectories)
                   : new string[] { path }
                   )
                   .OrderBy(str => str);
        }
        private void loadimgs(IEnumerable<string> paths)
        {
            wrapPanelChangePosition1.RemoveElements();
            paths.Select((path, i) => { loadOne(path, i + 1); return 1; }).ToArray();
        }
        private void loadOne(string path, int i)
        {
            var newPath = Common.changeFileNameInplace(path, oldname =>
                {
                    return string.Format(txtPrefix.Text + txtIndexFormat.Text, i);

                });
            var config = (lstSizeItems.SelectedValue as SizeItemCtrol).DataContext as SizeItemConfig;
            ImageCutter imgc = new ImageCutter(path, newPath, config.Width, config.Height, null);
            imgc.Width = 200;
            imgc.Height = 200;
            wrapPanelChangePosition1.AddElement(imgc);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            wrapPanelChangePosition1.Items.First().RectHeight = 50;
            wrapPanelChangePosition1.Items.First().RectWidth = 50;
        }

        private void lstSizeItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dropedPaths != null)
            {
                loadimgs(dropedPaths);
            }
        }

        private void btnCut_Click(object sender, RoutedEventArgs e)
        {
            foreach (var cutter in wrapPanelChangePosition1.Items)
            {
                cutter.Cut();
            }
            MessageBox.Show("完成");
        }

    }
}
